var url = "http://182.92.175.40:8080/WisdomCloudStore";
var accessToken = "111";
var now = new Date();
var startTime = parseInt(now.getTime() / 1000 - 24 * 3600 * 7);
var endTime = parseInt(now.getTime() / 1000 - 24 * 3600);
var NO_DATA = "nodata";
var TOKEN = "accessToken";
var dataType = "1";
if ($.cookie(TOKEN)) {
	accessToken = $.cookie(TOKEN);
}
var EXPIRES_TIME = getIDStorageTime(); //cookie保存时间n天
function getIDStorageTime() {
	var storageTime = new Date();
	//设置超时时间
	storageTime.setTime(storageTime.getTime() + 1800 * 1000);
	return storageTime;
}
//返回设定的超时时间 ：当天24点
function getExpireTime() {
	var expireTime = new Date();
	expireTime.setHours(24, 0, 0, 0);
	//	expireTime.setTime(expireTime.getTime() + 4000);
	return expireTime;
}

function selectData(dataType, callback) {
	var cookieName = accessToken + "_" + dataType + "_" + startTime + "_" + endTime;
	var old = $.cookie(cookieName);
	if (old) {
		var oldData = JSON.parse(old);
		callback(oldData, cookieName);
	} else {
		//		console.log(url + "/getShopAnalyInfo?accessToken=" + accessToken + "&data_type=" + dataType + "&start_time=" + startTime + "&end_time=" + endTime);

		$.ajax({
			type: "post",
			url: url + "/getShopAnalyInfo",
			async: true,
			data: {
				"accessToken": accessToken,
				"data_type": dataType,
				"start_time": startTime,
				"end_time": endTime
			},
			dataType: "json",
			crossDomain: true,
			success: function(data) {

				if (data.retCode == 0) {
					callback(data, cookieName);
				} else if (data.retCode == "40000") {
					callback(NO_DATA, cookieName);
				} else {
					console.log("发生错误，错误原因是：" + data.retInfo);
				}
			},
			error: function(obj, msg, msg2) {
				console.log("errorMsg:" + msg);
			},
			beforeSend: function() {
				$(".load-txt").show();
			},
			complete: function() {
				$(".load-txt").hide();
			}
		});
	}

}
$(function() {
	$.timeCount = function() {};
	$.extend($.timeCount, {
		dateTime: 0
	}, {
		count: 0
	});

	if ($.cookie(TOKEN) && $.cookie(TOKEN) != "" && $.cookie(TOKEN) != "null") {
		selectData("1", setData);
	}

	$(".user-data-type li:nth-child(1)").addClass("selected");
	$(".user-data-type li").click(function() {
		var ii = $(this).index();
		dataType = $(this).attr("data-type");
		$(".user-data-type li").each(function(index) {
			if (index == ii) {
				$(this).addClass("selected");
			} else {
				$(this).removeClass("selected");
			}

		});
		selectData(dataType, setData);

	});

	function setData(newData, cookieName) {
		timeArray = new Array();
		if (newData == NO_DATA) {
			showChart(timeArray);
			return;
		}
		$("#new").text(newData.result.new);
		$("#active").text(newData.result.active);
		$("#register").text(newData.result.register);
		$("#total").text(newData.result.total);
		$("#binding").text(newData.result.binding);
		var objArray = newData.result.data;
		$.each(objArray, function(index, value) {
			var count = new $.timeCount();
			count.dateTime = value.date;
			count.count = value.count;
			timeArray.push(count);
		});
		timeArray.sort(function(a, b) {
			return (a.dateTime - b.dateTime);
		});
		showChart(timeArray);
		var dd = JSON.stringify(newData);
		//如果cookie 是null，重新缓存
		if ($.cookie(cookieName) == null) {
			$.cookie(cookieName, dd, {
				expires: getExpireTime()
			});
		}

	}

	function showChart(dataArray) {
		var param1 = new Array();
		var param2 = new Array();
		$.each(dataArray, function(index, value) {
			var d = new Date(value.dateTime * 1000);
			param1.push((d.getMonth() + 1) + "." + d.getDate());
			param2.push(parseInt(value.count));
		});
		$('#container_line').highcharts({
			chart: {
				type: 'spline'
			},
			title: {
				text: ' '
			},
			subtitle: {
				text: ' '
			},
			legend: {
				enabled: false
			},
			tooltip: {
				pointFormat: '<b>{point.y:.0f}</b>'
			},
			xAxis: {
				categories: param1

			},
			yAxis: {
				min: 0,
				title: ''
			},
			series: [{
				name: '',
				data: param2
			}],
			credits: {
				enabled: false
			}
		});
	}

	var startDate = new Date(startTime * 1000);
	var endDate = new Date(endTime * 1000);

	$(".date-png").click(function() {
		$('#config-demo').click();
	});
	$('#config-demo').daterangepicker({
		"startDate": getFormatDate(startDate),
		"endDate": getFormatDate(endDate)
	}, function(start, end, label) {
		if (start >= now.getTime()) {
			start = now.getTime() - 3600 * 24 * 1000;
		}
		if (end >= now.getTime()) {
			end = now.getTime() - 3600 * 24 * 1000;
		}

		var startDate = new Date(start);
		var endDate = new Date(end);
		startDate.setHours(0, 0, 0, 0);
		endDate.setHours(23, 59, 59, 0);
		startTime = startDate.getTime() / 1000;
		endTime = endDate.getTime() / 1000;
		selectData(dataType, setData);

		//		console.log("New date range selected: " + start.format('YYYY-MM-DD') + " to " + end.format('YYYY-MM-DD') + " (predefined range: " + label + ")");
		//		console.log("New date range selected: " + start + " to " + end + " (predefined range: " + label + ")");
	});

	$(".logout").click(function() {
		if (confirm("确定要退出吗？")) {
			//是的，退出，清空cookie
			$.cookie(TOKEN, null);
			accessToken = "";
			location.href = "./index.html";
		} else {
			//否，不退出
			return false;
		}

	});

});

function getFormatDate(dateTime) {
	return (dateTime.getMonth() + 1) + "/" + dateTime.getDate() + "/" + dateTime.getFullYear();

}